#include <stdio.h>
#include <string.h>
#include <time.h>
#include <staticlibinit_gcce.h>

#include "sqlite3.h"


int main(void)
{
	printf("Open C DLL Test for sqlite!\n");
	printf("Sqlite Version %s!\n", sqlite3_libversion());
	
	sqlite3 *db = NULL;
	int result = sqlite3_open("C:\\data\\miliao\\test.sqlite", &db);
			
	char tmp [512];
	char sql[512];
	char *ptr = tmp; 
	sqlite3_stmt *statement = NULL;
	
	result = sqlite3_exec(db, "pragma synchronous = off", NULL, NULL, &ptr);

	sprintf(sql, "%s", "create table performance_test (column1 TEXT, column2 INTEGER, column3 INTEGER, column4 INTEGER, column5 INTEGER, column6 INTEGER, column7 TEXT, column8 FLOAT, column9 TEXT, column10 TEXT)");
	result = sqlite3_prepare(db, (const char*)sql, (int)strlen(sql), &statement, (const char **)&ptr);
	result = sqlite3_step(statement);
	result = sqlite3_finalize(statement);

	printf("begin to insert 10000 records\n");
	time_t begin = time(NULL);
	for (int i = 0; i < 50; i++){
		result = sqlite3_exec(db, "begin transaction", NULL, NULL, &ptr);
		for (int i = 0; i < 100; i++)
		{
			sprintf(sql, "%s", "insert into performance_test values ('Hallo Welt', 1, 2, 3, 4, 5, 'hello', 3.723, '2011-4-13 11:20:43', 'this is a long message to test sqlite performance, hold on there, dont let me done, babe')");
			//result = sqlite3_prepare(db, (const char*)sql, (int)strlen(sql), &statement, (const char **)&ptr);
			//result = sqlite3_step(statement);
			//result = sqlite3_finalize(statement);
			result = sqlite3_exec(db, sql, NULL, NULL, &ptr);
		}
		result = sqlite3_exec(db, "commit transaction", NULL, NULL, &ptr);
		if (result != SQLITE_OK){
			printf("Error: %s\n", ptr);
		}else
			printf("insert 100 records\n");
		fflush(stdout);
	}
	time_t end = time(NULL);
	printf("time cost for inserting 10000 records: %d seconds\n", (end - begin));
	
	result = sqlite3_close(db);
	printf("Press a character to exit!");
	int c = getchar();
	return 0;
}
 
